package com.l024.loanthief.customView;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.l024.loanthief.R;
import com.l024.loanthief.utils.DensityUtil;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/15 16:34
 * @Notes
 */
public class RadarView extends View {
    private Context mContext;

    private int radarColor;//雷达背景颜色
    private int lineColor;//线的颜色
    private int circleNum;//圆的个数
    private int firstCircleR;//第一个圆的半径
    private int differR;//每个圆半径相差
    private int spotColor;//点的颜色
    private int spotR;//点的半径

    private int centerPoint;//中心点

    private Paint mPaint;

    private int startAngle = -120;
    private ValueAnimator scanAnimator;//动画

    private SparseArray<SpotCoordinate> spots;//扫描点


    public RadarView(Context context) {
        this(context,null);
    }

    public RadarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RadarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadarView);
        radarColor = a.getInteger(R.styleable.RadarView_radarColor, Color.rgb(255,0,0));
        lineColor = a.getInteger(R.styleable.RadarView_lineColor,Color.rgb(255,255,255));
        circleNum = a.getInteger(R.styleable.RadarView_circleNum,5);
        if(circleNum<1){
            circleNum = 5;
        }
        firstCircleR = a.getInteger(R.styleable.RadarView_firstCircleR,DensityUtil.px2dip(mContext,40));
        spotColor = a.getInteger(R.styleable.RadarView_spotColor,Color.rgb(255,255,0));
        spotR = a.getInteger(R.styleable.RadarView_spotR,DensityUtil.px2dip(mContext,20));
        a.recycle();
        //每个圆圈直接间隙直接就是半径
        differR = firstCircleR;
        //初始化画笔
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(2);

        spots = new SparseArray<SpotCoordinate>();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = getMeasuredWidth();
        int measureHeight = getMeasuredHeight();
        //宽
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        //高
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //
        switch (widthMode) {
            case MeasureSpec.EXACTLY://相当于我们设置为match_parent或者为一个具体的值
                measureWidth = widthSize;
                measureHeight = heightSize;
                break;
            case MeasureSpec.AT_MOST://相当于我们设置为wrap_content
                //计算宽度
                if(circleNum>1){
                    measureWidth = ((circleNum-1)*differR+firstCircleR) * 2;
                }else{
                    measureWidth = firstCircleR * 2;
                }
                measureHeight = measureWidth;
                break;
            case MeasureSpec.UNSPECIFIED://无限制 例如RecyclerView
                break;
        }
        //计算组件的中心位置并返回组件最大宽度
        measureWidth = initCenterXY(measureWidth);
        setMeasuredDimension(measureWidth,measureWidth);
    }

    /**
     * 计算中心位置
     */
    private int initCenterXY(int measureWidth) {
        //获取屏幕宽度
        int widthPixels = DensityUtil.getWidthPixels(mContext);
        int rootWidth = getWidth();
        //判断组件宽度是否大于了屏幕宽度
        if(rootWidth>widthPixels){
            //重新设置宽度
            measureWidth = widthPixels;
            //中心位置等于屏幕中心
            centerPoint = widthPixels/2;
            //重新计算圆的半径
            firstCircleR = centerPoint/circleNum;
            differR = firstCircleR;
        }else{
            //小于屏幕宽度
            if(circleNum>1){
                centerPoint = ((circleNum-1)*differR+firstCircleR);
            }else{
                centerPoint = firstCircleR;
            }
        }
        return measureWidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制背景圆
        drawBgCircle(canvas);
        ////绘制坐标
        drawCoordinate(canvas);
        //绘制扫描
        drawSector(canvas);

        //添加扫描点
        if(startAngle==180){
            randomSpot(canvas);
        }else{
            //显示数组保存上一次的点
            if(spots!=null&&spots.size()>0){
                for(int i=0;i<spots.size();i++){
                    mPaint.setColor(spotColor);
                    canvas.drawCircle(spots.get(i).getX(),spots.get(i).getY(),spotR,mPaint);
                }
            }
        }

    }
    //绘制坐标
    private void drawCoordinate(Canvas canvas) {
        //先绘制十字
        mPaint.setColor(lineColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawLine(0,centerPoint,centerPoint*2,centerPoint,mPaint);
        canvas.drawLine(centerPoint,0,centerPoint,centerPoint*2,mPaint);
        //绘制圆
        for(int i=0;i<circleNum;i++){
            int r = (i-1)*differR+firstCircleR;
            mPaint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(centerPoint,centerPoint,r,mPaint);
        }

    }

    //绘制背景圆
    private void drawBgCircle(Canvas canvas) {
        mPaint.setColor(radarColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(centerPoint,centerPoint,centerPoint,mPaint);
    }

    /**
     * 绘制圆弧扇形
     */
    private void drawSector(Canvas canvas){
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAlpha(200);
        Shader shader =new LinearGradient(0, 0, centerPoint*2, centerPoint*2, Color.RED, Color.GREEN, Shader.TileMode.MIRROR);
        mPaint.setShader(shader);
        RectF oval = new RectF( 0, 0,
                centerPoint*2,centerPoint*2);
        canvas.drawArc(oval,startAngle,60,true,mPaint);
        //canvas.drawArc(oval,startAngle+120,60,true,mPaint);
        //canvas.drawArc(oval,startAngle+240,60,true,mPaint);
        mPaint.setAlpha(255);
        mPaint.setShader(null);
    }

    /**
     * 随机生产红色圆点
     */
    private void randomSpot(Canvas cavans){
        //随机生成2-circleNum
        int number = (int) (Math.random()*(circleNum-2)+2);
        mPaint.setColor(spotColor);
        for(int i=0;i<number;i++){
            //生成红点坐标
            int x =  (int) (Math.random()*(centerPoint*2-100)+100);
            int y =  (int) (Math.random()*(centerPoint*2-100)+100);
            cavans.drawCircle(x,y,spotR,mPaint);
            spots.append(i,new SpotCoordinate(x,y));
        }
    }

    //添加扫描动画
    public void startScan(){
        scanAnimator = ValueAnimator.ofInt(-120,240);
        scanAnimator.setInterpolator(new LinearInterpolator());//匀速
        scanAnimator.setRepeatCount(ValueAnimator.INFINITE);
        scanAnimator.setRepeatMode(ValueAnimator.RESTART);
        scanAnimator.setDuration(5000);
        scanAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                startAngle = (int)animation.getAnimatedValue();
                invalidate();
            }
        });
        scanAnimator.start();
    }

    public void stopScan(){
        if(scanAnimator!=null){
            scanAnimator.reverse();
            scanAnimator.clone();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(scanAnimator!=null){
            scanAnimator.reverse();
            scanAnimator.clone();
        }
    }

    //点坐标
    private class SpotCoordinate{
        private int x;
        private int y;

        public SpotCoordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }
}
